package com.quyeying.utils.result;

import lombok.Data;

/**
 * @ClassName R
 * @Description TODO
 * @Author skx
 * @Date 2020/10/20
 */
@Data
public class R {

    private Integer code;
    private String msg;
    private Object data;

    private static R r = new R();

    /**成功且带数据**/
    public static R success(Object object){
        r.setCode(ResultEnum.SUCCESS.getCode());
        r.setMsg(ResultEnum.SUCCESS.getMsg());
        r.setData(object);
        return r;
    }

    /**成功但不带数据**/
    public static R success(){
        r.setCode(ResultEnum.SUCCESS.getCode());
        r.setMsg(ResultEnum.SUCCESS.getMsg());
        r.setData(null);
        return r;
    }

    /**失败**/
    public static R error(Integer code,String msg){
        r.setCode(code);
        r.setMsg(msg);
        r.setData(null);
        return r;
    }

    public static R error(ResultEnum resultEnum){
        r.setCode(resultEnum.getCode());
        r.setMsg(resultEnum.getMsg());
        r.setData(null);
        return r;
    }

    /**失败带数据**/
    public static R error(Integer code,String msg, Object object){
        r.setCode(code);
        r.setMsg(msg);
        r.setData(object);
        return r;
    }

    public static R error(ResultEnum resultEnum, Object object){
        r.setCode(resultEnum.getCode());
        r.setMsg(resultEnum.getMsg());
        r.setData(object);
        return r;
    }

    @Override
    public String toString() {
        return "{" +
                "\"code\":" + code +
                ", \"msg\":\"" + msg + '\"' +
                ", \"data\":" + data +
                '}';
    }
}
